import { http } from "@/utils/http";

// 菜单信息
export interface Menu {
  id: number;
  name: string;
  parentId: number;
  path: string;
  component: string;
  title: string;
  icon?: string;
  rank?: number;
  status?: number;
  auths?: string[];
  all_auths?: string[];
  selected_auths?: string[];
  children?: Menu[];
  meta?: {
    title: string;
    icon?: string;
    auths?: string[];
    rank?: number;
  };
}

// 创建菜单请求
export interface MenuCreateRequest {
  name: string;
  parentId: number;
  path: string;
  component?: string;
  title: string;
  icon?: string;
  rank?: number;
  status?: number;
  auths?: string[];
}

// 更新菜单请求
export interface MenuUpdateRequest extends MenuCreateRequest {
  id: number;
}

// 获取菜单列表
export const getMenuList = () => {
  return http.request<{
    code: number;
    msg: string;
    data: Menu[];
  }>("post", "/investor/permissions");
};

// 创建菜单
export const createMenu = (data: MenuCreateRequest) => {
  return http.request<{
    code: number;
    msg: string;
  }>("post", "/investor/permissionadd", { data });
};

// 更新菜单
export const updateMenu = (data: MenuUpdateRequest) => {
  return http.request<{
    code: number;
    msg: string;
  }>("post", "/investor/permissionupdate", { data });
};

// 删除菜单
export const deleteMenu = (id: number) => {
  return http.request<{
    code: number;
    msg: string;
  }>("post", "/investor/permissiondel", { data: { id } });
};

// 获取用户菜单(登录后调用)
export const getUserMenus = () => {
  return http.request<{
    code: number;
    msg: string;
    data: Menu[];
  }>("post", "/investor/user/menus");
};

// 获取管理员拥有的权限
export const getAdminPermissions = (adminId: number) => {
  return http.request<{
    code: number;
    msg: string;
    data: number[]; // 权限ID列表
  }>("post", "/investor/adminpermissions", { data: { "admin-id": adminId } });
};

// 设置管理员权限
export const setAdminPermissions = (data: { admin_id: number; permission_ids: number[] }) => {
  return http.request<{
    code: number;
    msg: string;
  }>("post", "/investor/adminpermissionsadd", { data });
};
